package com.qdxht.dispenser.ui.activity.main;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.SeekBar;

import com.hjq.toast.ToastUtils;
import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.databinding.ActivityQrcodeSettingsBinding;
import com.qdxht.dispenser.utils.BroadcastUtil;
import com.qdxht.dispenser.utils.ConfigUtil;

/**
 * 二维码设置页面
 */
public class QRCodeSettingsActivity extends BaseActivity<ActivityQrcodeSettingsBinding> {

    // 屏幕宽高
    private int screenWidth;
    private int screenHeight;

    public static void start(Context context) {
        Intent intent = new Intent(context, QRCodeSettingsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected ActivityQrcodeSettingsBinding getBinding() {
        return ActivityQrcodeSettingsBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        // 获取屏幕尺寸
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;

        // 设置横向偏移滑动条
        binding.sbHorizontalOffset.setMax(screenWidth);
        binding.sbHorizontalOffset.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 更新进度值显示
                binding.hOffset.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 开始滑动
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 停止滑动
            }
        });

        // 设置纵向偏移滑动条
        binding.sbVerticalOffset.setMax(screenHeight);
        binding.sbVerticalOffset.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 更新进度值显示
                binding.vOffset.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 开始滑动
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 停止滑动
            }
        });

        // 设置保存按钮点击事件
        binding.btnSave.setOnClickListener(v -> saveSettings());

        // 设置重置按钮点击事件
        binding.btnReset.setOnClickListener(v -> resetSettings());
        
        // 设置预览按钮点击事件
        binding.btnPreview.setOnClickListener(v -> previewQRCode());
    }

    @Override
    protected void initData() {
        // 获取当前设置
        int qrCodeSize = ConfigUtil.getQRCodeSize();
        int horizontalOffset = ConfigUtil.getQRCodeHorizontalOffset();
        int verticalOffset = ConfigUtil.getQRCodeVerticalOffset();
        
        // 显示当前设置
        binding.etQrcodeSize.setText(String.valueOf(qrCodeSize));
        binding.sbHorizontalOffset.setProgress(horizontalOffset);
        binding.sbVerticalOffset.setProgress(verticalOffset);
        
        // 设置偏移值文本显示
        binding.hOffset.setText(String.valueOf(horizontalOffset));
        binding.vOffset.setText(String.valueOf(verticalOffset));
    }

    /**
     * 保存设置
     */
    private void saveSettings() {
        String sizeStr = binding.etQrcodeSize.getText().toString().trim();
        if (TextUtils.isEmpty(sizeStr)) {
            ToastUtils.show("请输入二维码大小");
            return;
        }

        try {
            int qrCodeSize = Integer.parseInt(sizeStr);
            if (qrCodeSize <= 0) {
                ToastUtils.show("二维码大小必须大于0");
                return;
            }

            // 保存设置
            ConfigUtil.setQRCodeSize(qrCodeSize);
            ConfigUtil.setQRCodeHorizontalOffset(binding.sbHorizontalOffset.getProgress());
            ConfigUtil.setQRCodeVerticalOffset(binding.sbVerticalOffset.getProgress());
            BroadcastUtil.sendQRCodeBroadcast(this);
            ToastUtils.show("保存成功");
            finish();
        } catch (NumberFormatException e) {
            ToastUtils.show("请输入有效的数字");
        }
    }

    /**
     * 重置设置
     */
    private void resetSettings() {
        // 重置为默认值
        binding.etQrcodeSize.setText(String.valueOf(ConfigUtil.DEFAULT_QRCODE_SIZE));
        binding.sbHorizontalOffset.setProgress(ConfigUtil.DEFAULT_QRCODE_HORIZONTAL_OFFSET);
        binding.sbVerticalOffset.setProgress(ConfigUtil.DEFAULT_QRCODE_VERTICAL_OFFSET);
        
        // 重置偏移值文本显示
        binding.hOffset.setText(String.valueOf(ConfigUtil.DEFAULT_QRCODE_HORIZONTAL_OFFSET));
        binding.vOffset.setText(String.valueOf(ConfigUtil.DEFAULT_QRCODE_VERTICAL_OFFSET));
    }
    
    /**
     * 预览二维码
     */
    private void previewQRCode() {
        String sizeStr = binding.etQrcodeSize.getText().toString().trim();
        if (TextUtils.isEmpty(sizeStr)) {
            ToastUtils.show("请输入二维码大小");
            return;
        }

        try {
            int qrCodeSize = Integer.parseInt(sizeStr);
            if (qrCodeSize <= 0) {
                ToastUtils.show("二维码大小必须大于0");
                return;
            }
            
            // 获取当前设置
            int horizontalOffset = binding.sbHorizontalOffset.getProgress();
            int verticalOffset = binding.sbVerticalOffset.getProgress();
            
            // 跳转到预览页面
            QRCodePreviewActivity.start(this, qrCodeSize, horizontalOffset, verticalOffset);
        } catch (NumberFormatException e) {
            ToastUtils.show("请输入有效的数字");
        }
    }
} 